$(document).ready(function() {
	/* select2初始化 */
	$("select.select2:not([data-ajax--url])").select2({
		theme : "bootstrap",
		language : "zh-CN",
		allowClear : true
	});
	$("select.select2[data-ajax--url]").select2({
		theme : "bootstrap",
		language : "zh-CN",
		allowClear : true,
		minimumInputLength : 1,
		ajax : {
			delay : 500,
			cache : true
		}
	});
	/* 富文本输入框初始化 */
	$(".textarea").wysihtml5({
		locale : 'zh-CN',
		toolbar : {
			size : 'sm',
			html : true,
			image : false
		}
	});
	/* icheck 初始化 */
	$('input.icheck').iCheck({
		checkboxClass : 'icheckbox_square-blue',
		radioClass : 'iradio_square-blue'
	});
	/* webuploader jquery化，以及添加bootstrap的进度条 */
	$.fn.webuploader = function(opt) {
		var $this = $(this);
		$this.data('_uploader_opt', $.extend({
			auto : true,
			resize : false,
			pick : $this.find('.picker'),
			server : '/api/upload/file.json',
			swf : '/assets/plugins/webuploader/Uploader.swf',
		}, opt));
		var uploader = WebUploader.create($this.data('_uploader_opt'));
		uploader.on('error', function(type) {
			if (type == 'Q_EXCEED_NUM_LIMIT') {
				layer.msg(opt.error_num || '文件太多', {
					icon : 2,
					time : 1500
				});
			} else if (type == 'Q_EXCEED_SIZE_LIMIT') {
				layer.msg(opt.error_size || '文件超出大小', {
					icon : 2,
					time : 1500
				});
			} else if (type == 'Q_TYPE_DENIED') {
				layer.msg(opt.error_type || '文件类型错误', {
					icon : 2,
					time : 1500
				});
			}
		});
		uploader.on('uploadStart', function(file) {
			$this.append('<div class="progress"><div class="progress-bar progress-bar-info progress-bar-striped" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" style="width: 0%"><span class="sr-only"></span></div></div>');
			$this.triggerHandler('uploadStart', [ file ]);
		});
		uploader.on('uploadProgress', function(file, progress) {
			$this.find('div.progress .progress-bar').css('width', (parseInt(progress * 100) + '%'));
			$this.find('div.progress .progress-bar span').text('已上传：' + parseInt(progress * 100) + '%');
		});
		uploader.on('uploadSuccess', function(file, response) {
			$this.find('input[data-value]').val(response.data);
			$this.triggerHandler('uploadSuccess', [ file, response ]);
		});
		uploader.on('uploadError', function(file, reason) {
			$this.triggerHandler('uploadError', [ file, reason ]);
		});
		uploader.on('uploadComplete', function(file) {
			$this.find('div.progress').remove();
			$this.triggerHandler('uploadComplete', [ file ]);
		});

		$this.on('upload', function(e, file) {
			uploader.upload(file);
		}).on('stop', function(e, file) {
			uploader.stop(file);
		}).on('cancelFile', function(e, file) {
			uploader.cancelFile(file);
		});

		$this.data('_uploader', uploader);

		return uploader;
	}
}).on('click', '[data-laydate]', function() {
	var $this = $(this);
	laydate($.extend({
		choose : function(datas) {
			if ($this.data('intervalStart')) {
				$('[data-laydate][data-interval-end="' + $this.data('intervalStart') + '"]').data('start', datas).data('min', datas);
				return;
			}
			if ($this.data('intervalEnd')) {
				$('[data-laydate][data-interval-start="' + $this.data('intervalEnd') + '"]').data('max', datas);
				return;
			}
		}
	}, $this.data()));
})
